package com.ujiuye.controller;

import com.ujiuye.bean.User;
import com.ujiuye.service.EnrollService;
import com.ujiuye.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 控制层,获取页面提交的数据
 */
@WebServlet("/regdo")
public class EnrollServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8"); // 处理中文乱码
        // 获取请求参数
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String name = req.getParameter("name");
        String phoneNo = req.getParameter("phoneNo");
        String birthday = req.getParameter("birthday");

        // 将请求参数封装成User对象,方便传递
        User user = new User(username, password, name, phoneNo, birthday);

        // 交给业务逻辑层处理
        EnrollService es = new EnrollService();
        // 调用业务逻辑层方法,返回注册结果
        boolean bo = es.register(user);

        // 控制页面的跳转,如果注册成功,即返回结果为true,则跳转到登陆页面,否则继续注册
        if(bo){
            // 跳转到登陆页面
            req.getRequestDispatcher("login.jsp").forward(req,resp);

        }else{ // 注册失败
            req.getRequestDispatcher("reg.html").forward(req,resp);
        }
    }

}
